package com.liuqi.business.controller.front;

import com.liuqi.base.BaseFrontController;
import com.liuqi.business.dto.SelectDto;
import com.liuqi.business.enums.SlideTypeEnum;
import com.liuqi.business.model.*;
import com.liuqi.business.service.*;
import com.liuqi.exception.NoLoginException;
import com.liuqi.redis.Captcha;
import com.liuqi.response.ReturnResponse;
import com.liuqi.third.zb.SearchPrice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 前台登录控制层
 */
@Api(description = "首页（不验证登录状态）")
@RestController
@RequestMapping("/search")
public class indexController extends BaseFrontController {
    @Autowired
    private ContentService contentService;
    @Autowired
    private SlideService slideService;
    @Autowired
    private VersionService versionService;
    @Autowired
    private CurrencyRechargeService currencyRechargeService;
    @Autowired
    private CurrencyExtractService currencyExtractService;
    @Autowired
    private CurrencyService currencyService;
    @Autowired
    private SearchPrice searchPrice;
    /**
     * @param request
     * @param response
     * @param lang
     * @return
     */
    @ApiOperation(value = "切换语言")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "lang", value = "语言 zh：中文  en:英文", required = true, paramType = "query")
    })
    @PostMapping("/changeLanauage")
    public ReturnResponse changeLanauage(HttpServletRequest request, HttpServletResponse response, @RequestParam("lang") String lang) {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if ("zh".equals(lang)) {
            localeResolver.setLocale(request, response, new Locale("zh", "CN"));
        } else if ("en".equals(lang)) {
            localeResolver.setLocale(request, response, new Locale("en", "US"));
        }
        request.getSession().setAttribute("lanauage", lang);
        return ReturnResponse.backSuccess();
    }

    /**
     * 查询公告
     *
     * @return
     */
    @ApiOperation(value = "查询公告")
    @PostMapping(value = "/indexContent")
    public ReturnResponse indexContent() {
        List<ContentModelDto> contentList = contentService.getNewContent(2);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("contentList", contentList);
        return ReturnResponse.backSuccess(map);
    }

    /**
     * 查询pc轮播图
     *
     * @return
     */
    @ApiOperation(value = "查询轮播图")
    @PostMapping(value = "/indexSlidesPC")
    public ReturnResponse indexSlidesPC() {
        List<SlideModelDto> slideList = slideService.findSlide(SlideTypeEnum.PC.getCode());
        return ReturnResponse.backSuccess(slideList);
    }

    /**
     * 查询app轮播图
     *
     * @return
     */
    @ApiOperation(value = "查询轮播图")
    @PostMapping(value = "/indexSlides")
    public ReturnResponse indexSlides() {
        List<SlideModelDto> slideList = slideService.findSlide(SlideTypeEnum.APP.getCode());
        return ReturnResponse.backSuccess(slideList);
    }


    //获取版本号
    @ApiOperation(value = "获取版本号")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "type", value = "类型0:安卓 1:ios", required = true, defaultValue = "0", paramType = "query")
    })
    @PostMapping("/appVersion")
    public ReturnResponse appVersion(@RequestParam(value = "type", defaultValue = "0") Integer type) {
        VersionModelDto version = versionService.getConfig();
        Map<String, Object> map = new HashMap<String, Object>();
        String appAddress = version.getAndroidAddress();
        String appVersion = version.getAndroidVersion();
        String updateInfo = version.getAndroidInfo();
        if (type == 1) {
            appAddress = version.getIosAddress();
            appVersion = version.getIosVersion();
            updateInfo = version.getIosInfo();
        }
        map.put("appAddress", appAddress);
        map.put("appVersion", appVersion);
        map.put("updateInfo", updateInfo);
        return ReturnResponse.backSuccess(map);
    }


    /**
     * 获取币种列表
     */
    @ApiOperation(value = "获取所有币种列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "type", value = "查询类型 1所有币种 2可以充值币种 3可以提现币种", required = true, defaultValue = "1", paramType = "query")
    })
    @PostMapping("/currencyList")
    public ReturnResponse currencyList(@RequestParam(value = "type", defaultValue = "1") Integer type, HttpServletRequest request) {
        List<SelectDto> curList = new ArrayList<>();
        if (type == 1) {
            List<CurrencyModelDto> list = currencyService.getUsing();
            for (CurrencyModelDto model : list) {
                curList.add(new SelectDto(model.getId(), model.getName()));
            }
        }else if (type == 2) {
            List<CurrencyRechargeModelDto> list = currencyRechargeService.getOnRechargeList();
            for (CurrencyRechargeModelDto model : list) {
                curList.add(new SelectDto(model.getCurrencyId(), model.getCurrencyName()));
            }
        } else if (type == 3) {
            List<CurrencyExtractModelDto> list = currencyExtractService.getOnExtractList();
            for (CurrencyExtractModelDto model : list) {
                curList.add(new SelectDto(model.getCurrencyId(), model.getCurrencyName()));
            }
        }
        curList=curList.stream().distinct().collect(Collectors.toList());
        return ReturnResponse.backSuccess(curList);
    }

    /**
     * 获取充币item
     */
    @ApiOperation(value = "获取充值地址")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "currencyId", value = "币种id", required = true, paramType = "query")
    })
    @PostMapping("/rechargeItem")
    public ReturnResponse rechargeItem(HttpServletRequest request, @RequestParam("currencyId") Long currencyId) throws NoLoginException {
        return ReturnResponse.backSuccess(currencyRechargeService.getOnRechargeList().stream().filter(t->t.getCurrencyId().equals(currencyId)).collect(Collectors.toList()));
    }

    /**
     * 获取提下nitem
     */
    @ApiOperation(value = "获取充值地址")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "currencyId", value = "币种id", required = true, paramType = "query")
    })
    @PostMapping("/extractItem")
    public ReturnResponse extractItem(HttpServletRequest request, @RequestParam("currencyId") Long currencyId) throws NoLoginException {
        return ReturnResponse.backSuccess(currencyExtractService.getOnExtractList().stream().filter(t->t.getCurrencyId().equals(currencyId)).collect(Collectors.toList()));
    }



    /**
     * 获取图片验证吗
     *
     * @return
     */
    @ApiOperation(value = "获取图片验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "key", value = "传入验证key", required = true, paramType = "query")
    })
    @PostMapping("/getImage")
    public ReturnResponse getImage(@RequestParam(value = "key",required = false,defaultValue = "")String key,
                                   HttpServletRequest request, HttpServletResponse response) {
        System.out.println(StringUtils.isEmpty(key) +"-"+ Captcha.hashKey(key));
        if(StringUtils.isEmpty(key) || !Captcha.hashKey(key)){
            key=UUID.randomUUID().toString().replace("-","");
        }
        return ReturnResponse.backSuccess(Captcha.saveCaptcha(key));
    }
    /**
     * 测试验证码
     *
     * @return
     */
    @ApiOperation(value = "获取图片验证吗")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "key", value = "传入验证key", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "code", value = "", required = true, paramType = "query")
    })
    @PostMapping("/checkImage")
    public ReturnResponse checkImage(@RequestParam(value = "key",required = false)String key,
                           @RequestParam(value = "code",required = true)String code,
                           HttpServletRequest request, HttpServletResponse response) throws Exception {
        String sessionId = request.getSession().getId();
        if(StringUtils.isEmpty(key)){
            key = sessionId;
        }
        if(StringUtils.isEmpty(key)){
            return ReturnResponse.backFail("key为空");
        }
        Captcha.checkCaptcha(key,code);
        return ReturnResponse.backSuccess("成功");
    }


    /**
     * usdt人民币价格
     */
    @ApiOperation(value = "usdt人民币价格")
    @PostMapping("/usdtPrice")
    public ReturnResponse usdtPrice(@RequestParam(value = "type", defaultValue = "1") Integer type, HttpServletRequest request) {
        BigDecimal price = searchPrice.getUsdtQcPrice();
        return ReturnResponse.backSuccess(price);
    }
}
